package book

import (
	"common/json_response"
	"gateway/internal/logic"
	"gateway/internal/types/v1/book"
	"github.com/gin-gonic/gin"
)

type BookController struct {
	logic *logic.BookLogic
}

// NewBookController 创建新的书架控制器
func NewBookController() *BookController {
	controller := &BookController{
		logic: logic.NewBookLogic(),
	}
	return controller
}

// Favourite 收藏漫画
func (bc BookController) Favourite(ctx *gin.Context) {
	var request book.FavouriteRequest
	response := json_response.NewJsonHttpResponse(ctx)
	if err := ctx.ShouldBind(&request); err != nil {
		response.SetMsg(err.Error()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	operate, ok := bc.logic.FavouriteAction(ctx, request.ComicId)
	if !ok {
		response.SetMsg(bc.logic.GetMessage()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	response.SetMsg("操作成功").SetData(gin.H{"operate": operate}).SetCode(json_response.CODE_SUCCESS).Send()
	return
}

// Index 收藏列表
func (b *BookController) Index(ctx *gin.Context) {
	var request book.IndexRequest
	response := json_response.NewJsonHttpResponse(ctx)
	if err := ctx.ShouldBind(&request); err != nil {
		response.SetMsg(err.Error()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	list, err := b.logic.GetListByPage(ctx, request.Page.Page, request.Page.PerPage)
	if err != nil {
		response.SetMsg(err.Error()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	response.SetData(list).SetCode(json_response.CODE_SUCCESS).Send()
	return
}
